package myJs.api

import myJs.myPkg.MyFetch
import myJs.tool.Tool
import org.scalajs.dom.FormData
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Pojo
import shared.pojo.Pojo.MemberData
import slinky.web.html.s
import upickle.default._

/**
 * Created by yz on 19/5/2022
 */
object MemberApi {

  import Pojo.member._

  val adminUrl = s"${Api.apiUrl}/panda/admin/member"

  def all = {
    val url = s"${Api.apiUrl}/panda/admin/member/all"
    val token = Tool.getToken
    MyFetch.getRnStr(url = s"${url}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer $token")).map { str =>
      read[List[ujson.Obj]](str)
    }
  }

  def update(request: Update.Request) = {
    val url = s"${adminUrl}/update"
    MyFetch.postRnText(url = s"${url}", data = write(request),
      headers = Map("Content-Type" -> "application/json",
        "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
    }
  }

  def add(request: Add.Request) = {
    val url = s"${adminUrl}/add"
    MyFetch.postRnText(url = s"${url}", data = write(request),
      headers = Map("X-Authorization" -> s"Bearer ${Tool.getToken}","Content-Type" -> "application/json")).map { str =>
      str
    }
  }

  def getById(idV: String) = {
    val url = s"${Api.apiUrl}/panda/admin/member/getById"
    MyFetch.getRnStr(url = s"${url}?id=${idV}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      read[MemberData](str)
    }
  }

  def delete(id: String) = {
    val url = s"${Api.apiUrl}/panda/admin/member/delete"
    MyFetch.deleteRnStr(url = s"${url}?id=${id}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }


}
